<template>
  <div class="login_container">
    <div class="login_title">
      <h1>Perfect Admin</h1>
    </div>
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/my-logo.png"/>
      </div>
       <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginFormRules"
          class="login_form"
        >
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              prefix-icon="iconfont icon-3702mima"
            ></el-input>
          </el-form-item>
           <el-form-item style="position:relative">
             <el-input
                     v-model="loginForm.captcha"
                     name="logVerify"
                     placeholder="请输入验证码"
                     style="width:60%"
                     @keyup.enter.native="login"
             ></el-input>
             <div class="vPic">
               <img
                       v-if="picPath"
                       :src="picPath"
                       alt="请输入验证码"
                       @click="getVefifyCode"
               />
             </div>
           </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login" class="loginbtn">登录</el-button>
          </el-form-item>
       </el-form>
      </div>
    <div class="footer">
      <div class="links">
        <a href="https://www.cnblogs.com/zhangyafei" target="_blank" title="博客"
        ><img src="@/assets/boke.gif" class="link-icon"
        /></a>
        <a href="https://gitee.com/zhangyafeii/PerfectAdmin" target="_blank" title="码云"
        ><img src="@/assets/gitee.jpg" class="link-icon"
        /></a>
        <a href="https://github.com/zhangyafeii" target="_blank" title="github"
        ><img src="@/assets/github.png" class="link-icon"
        /></a>
      </div>
      <div class="copyright">
        Copyright &copy; {{ curYear }}  💖  zhangyafei
      </div>
    </div>
  </div>
</template>

<script>
import Qs from "qs"

export default {
  data(){
    return {
      loginForm: {
        username: 'admin',
        password: '000000',
        captcha: '',
        captchaId: '',
      },
      // 表单验证
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      },
      picPath: '',
      curYear: 0,

    }
  },
  created(){
    this.getVefifyCode();
    this.curYear = new Date().getFullYear();
  },
  methods: {
    login() {
      var v1 = this;
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return;
        const {data: res, status: status} = await this.axios.post("login", v1.loginForm);
        if (status !== 200)  return v1.$message.error(res.msg);
        v1.$message.success(res.msg);
        sessionStorage.setItem("token", res.token);
        v1.$store.commit('handleUserID', res.data.id);
        v1.$store.commit('handleUserName', res.data.username);
        v1.$store.commit('handleNickName', res.data.nickname);
        v1.$store.commit('handleRole', res.data.role);
        if (res.data.avatar_url === '') {
          var avatar = require('../assets/avatar.jpg')
        } else {
          var avatar = v1.domain + res.data.avatar_url
        }
        v1.$store.commit('handleAvatar', avatar);
        v1.$router.push("/home").catch(err => {err})
        })
    },
    async getVefifyCode() {
      const {data: res, status: status} = await this.axios.post("base/captcha");
      if (status !== 200) return this.$message.error("获取验证码失败");
      this.picPath = this.axios.defaults.baseURL + res.data.PicPath;
      this.loginForm.captchaId = res.data.CaptchaId;
    },
  }
}
</script>

<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  background-image: url("../assets/images/background2.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login_title{
  position: absolute;
  top: 5%;
  color: white;
}
.login_box{
  position: relative;
  height: 360px;
  width: 400px;
  background-color: steelblue;
  border-radius: 3px;
}
.avatar_box{
  width: 130px;
  height:130px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position: relative;
  left: 33%;
  bottom: 50px;
  img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login_form {
  padding: 0 15px;
  position: relative;
  bottom: 20px;
  box-sizing: border-box;
}
.vPic {
  width: 33%;
  height: 38px;
  float: right !important;
  background: #ccc;
img {
  cursor: pointer;
  vertical-align: middle;
}
}
.loginbtn{
  width: 100%;
  margin-left: 0;
  height: 45px;
}
.footer {
  position: absolute;
  bottom: 20px;
  width: 400px;
  height: 100px;
  text-align: center;
  /*background-color: steelblue;*/
}
.links {
  padding-top: 20px;;
  margin-bottom: 20px;
  font-size: 14px;
  width: 330px;
  display: inline-flex;
  flex-direction: row;
  justify-content: space-between;
  padding-right: 20px;
}
.link a {
      color: rgba(0, 0, 0, 0.45);
      transition: all 0.3s;
}
.copyright {
    /*text-align: center;*/
    color: white;
    /*color: rgba(0, 0, 0, 0.45);*/
    font-size: 14px;
}

.link-icon {
  width: 20px;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
}
</style>
